package com.dqlp.yf.rabbitmq.config;

import com.dqlp.yf.rabbitmq.constant.MqConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;

import java.util.HashMap;
import java.util.Map;


/**
 * @program: rabbitmq
 * @description: rabbitmq配置文件
 * @author: andy
 * @create: 2019-09-07 10:18
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Queue createQueue() {
        return new Queue(MqConstant.TEST_QUEUE,true);
    }
    @Bean
    public DirectExchange createExchange() {
        return new DirectExchange(MqConstant.TEST_EXCHANGE, true, false);
    }
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(createQueue()).to(createExchange()).with(MqConstant.ROUTERKEY);
    }





    @Bean
    public Queue directQueue() {
        Map<String, Object> agruments = new HashMap<String, Object>();
        agruments.put("x-dead-letter-exchange", MqConstant.DLX_EXCHANGE);
        agruments.put("x-message-ttl", 5000);
        return new Queue(MqConstant.DIRECT_QUEUE,true,false,false,agruments);
    }
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(MqConstant.DIRECT_EXCHANGE, true, false);
    }
    @Bean
    public Binding directBinding() {
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(MqConstant.DIRECT_ROUTERKEY);
    }

    @Bean
    public Queue dlxQueue() {
        return new Queue(MqConstant.DLX_QUEUE,true);
    }
    @Bean
    public TopicExchange dlxExchange() {
        return new TopicExchange(MqConstant.DLX_EXCHANGE, true, false);
    }
    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("#");
    }



    //延迟队列
    @Bean
    public TopicExchange lazyExchange(){
        TopicExchange exchange = new TopicExchange(MqConstant.LAZY_EXCHANGE, true, false);
        exchange.setDelayed(true);  //开启延迟队列
        return exchange;
    }

    @Bean
    public Queue lazyQueue(){
        return new Queue(MqConstant.LAZY_QUEUE, true);
    }

    @Bean
    public Binding lazyBinding(){
        return BindingBuilder.bind(lazyQueue()).to(lazyExchange()).with(MqConstant.LAZY_KEY);
    }













/*
*如果需要在生产者需要消息发送后的回调，需要对rabbitTemplate设置ConfirmCallback对象，由于不同的生产者需要对应不同的ConfirmCallback，如果rabbitTemplate设置为单例bean，则所有的rabbitTemplate
实际的ConfirmCallback为最后一次申明的ConfirmCallback
所以这里必须为prototype类型  要不然回报错
*/
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMandatory(true);
        template.setMessageConverter(new SerializerMessageConverter());
        return template;
    }

 }
